package cn.edu.ljl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Writer;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


//对成绩信息进行更新，提供修改界面，在输入框中已有原数据信息

public class update extends HttpServlet {

    public static String ID ;

    public String getID() {
        return ID;
    }

    public void setID(String ID) {
        this.ID = ID;
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        ID=id;

        String sql = "SELECT * FROM grade WHERE id=" + id;
        try {
            List<Student> students = DBMS.getStudents(sql);
            if (students.size() == 0) {
                response.sendRedirect("./list");
                return;
            }

            Student student = students.get(0);

            StringBuilder sb = new StringBuilder();
            sb.append(Page.getHeader());
            sb.append(buildStudentForm(student));
            sb.append(Page.getEnd());

            response.setCharacterEncoding("UTF-8");
            try (Writer writer = response.getWriter()) {
                writer.write(sb.toString());
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public String buildStudentForm(Student student) {
        String html = String.format("<center>\n" +
                        "<br>\n" +
                        "<form action='./getupdate' method='POST''>\n" +
                        "<input type='hidden' name='id' value='%s'>" +
                        "<table style='background-color:#dddddd;padding:2em'>\n" +
                        "\t<tr><td>学号:</td><td><input type='text' name=numbb    value='%s'></td></tr>\n" +
                        "\t<tr><td>名字:</td><td><input type='text' name=nam      value='%s'></td></tr>\n" +
                        "\t<tr><td>科目:</td><td><input type='text' name=subje    value='%s'></td></tr>\n" +
                        "\t<tr><td>分数:</td><td><input type='text' name=score    value='%s'></td></tr>\n"+
                        "\t<tr><td>感想:</td><td><textarea name=feel cols='50' rows='5'>%s</textarea></td></tr>\n" +
                        "\t<tr><td colspan='2' align='center'><br><input type='submit' value='提交修改'></td></tr>\n" +
                        "</table>\t\n" +
                        "</form>\n" +
                        "</div>\n" +
                        "</center>",
                student.getID(),student.getNumber(), student.getName(), student.getSubject(), student.getScore(), student.getFeel()
        );
        return html;
    }

















//        request.setCharacterEncoding("UTF-8");
//        String id = request.getParameter("id");//获取修改的ID
//        System.out.println(id);
//        setID(id);
//        response.sendRedirect("./change.html");

    }

//
//        String number = request.getParameter("numbb");
//        String name   = request.getParameter("nam");
//        String subject= request.getParameter("subje");
//        String score  = request.getParameter("score");
//        String feel   = request.getParameter("feel");
//
//        String id1 = (String) ids.get(0);
//        System.out.println(id1);
//
//        String sql = String.format(UPDATE_TEMPLATE,number,name,subject,score,feel);
//        String sql1 = sql + id1;
//        System.out.println(sql1);
//
//        try {
//            DBMS.update(sql1);
//            response.sendRedirect("./list");
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }
//

        //String sql="update grade set nam = '李俊霖' where id = 10;";


//update grade set numbb='66',nam ='李俊霖',subje='计算机',score=100,feel='加油乐观'  where id = 10;